﻿package leaf.science.ui.camera
{

import flash.events.MouseEvent;
import flash.geom.Point;


public class DemosCamera extends Camera
{
	
	public var h1:DemoHotspot;
	public var h2:DemoHotspot;
	public var h3:DemoHotspot;
	public var h4:DemoHotspot;
	public var h5:DemoHotspot;
	public var h6:DemoHotspot;
	public var h7:DemoHotspot;
	public var h8:DemoHotspot;
	
	public var hotspots:Array;
	private var _currentHotspot:DemoHotspot;
	

	public function DemosCamera()
	{
		stop();
		mouseEnabled = false;
		
		hotspots = [h1, h2, h3, h4, h5, h6, h7, h8];
		
		h1.id = 0;
		h1.frame = 51;
		
		h2.id = 1;
		h2.frame = 1;
		
		h3.id = 2;
		h3.frame = 64;
		
		h4.id = 3;
		h4.frame = 77;
		
		h5.id = 4;
		h5.frame = 16;
		
		h6.id = 5;
		h6.frame = 1;
		
		h7.id = 6;
		h7.frame = 1;
		
		h8.id = 7;
		h8.frame = 91;
		
		this.addEventListener( MouseEvent.MOUSE_OVER, onHotSpotOver, false, 0, true );
		this.addEventListener( MouseEvent.MOUSE_OUT, onHotSpotOut, false, 0, true );
//		this.addEventListener( MouseEvent.CLICK, onMouseClick, false, 0, true );
	}
	
	public function showToolTip( h:DemoHotspot ):void
	{
		var pos:Point = new Point( h.x, h.y );
		//var pos:Point = camera.localToGlobal( new Point( h.x, h.y ));
		
		if( !h.toolTip )
		{
			h.toolTip = new DemoToolTip( h );;
			h.toolTip.x = pos.x;
			h.toolTip.y = pos.y;
			addChild( h.toolTip );
		}
		h.toolTip.open();
	}
	
	public function closeToolTip( h:DemoHotspot ):void
	{
		if( h && h.toolTip )
		{
			h.toolTip.close();
		}
	}
	
	public function closeAllToolTips():void
	{
		for( var i:Number=0; i<hotspots.length; i++ )
		{
			closeToolTip( DemoHotspot( hotspots[i] ));
		}
	}

	public function closeAllToolTipsNow():void
	{
		for( var i:Number=0; i<hotspots.length; i++ )
		{
			var h:DemoHotspot = hotspots[i];
			
			if( h.toolTip )
				h.toolTip.closeNow();
		}
	}
	
//	private function onMouseClick( evt:MouseEvent ):void
//	{
//		if( evt.target is DemoHotspot )
//		{
//			this.hotspot = evt.target as DemoHotspot;
//		}
//	}

	private function onHotSpotOver( evt:MouseEvent ):void
	{
		if( evt.target is DemoHotspot )
		{
//			trace( "OVER " + evt.target );
			closeAllToolTips();
			var h:DemoHotspot = evt.target as DemoHotspot;
			showToolTip( h );
		}
	}
	
	private function onHotSpotOut( evt:MouseEvent ):void
	{
		if( evt.target is DemoHotspot )
		{
//			trace( "OUT " + evt.target );
			var h:DemoHotspot = evt.target as DemoHotspot;
			
			if( h.toolTip )
				h.toolTip.close();
		}
	}
	
	public function set hotspot( h:DemoHotspot ):void
	{
		_currentHotspot = h;
		gotoFrame( h.frame );
	}
	
	override public function onMotionEnd():void
	{
		showToolTip( _currentHotspot );
	}
	
	public function get hotspot():DemoHotspot
	{
		return( _currentHotspot );
	}
	
	public function set hotspotId( id:uint ):void
	{
		var h:DemoHotspot = getHotspotById( id );
		if( h )	this.hotspot = h;
	}

	private function getHotspotById( id:uint ):DemoHotspot
	{
		for( var i:Number=0; i<hotspots.length; i++ )
		{
			if( DemoHotspot( hotspots[i] ).id == id )
				return( hotspots[i] );			
		}
		return( null );
	}
}
}